//
//  CollectionView.m
//  StarFortune
//
//  Created by student on 14-10-13.
//  Copyright (c) 2014年 niit. All rights reserved.
//

#import "CollectionView.h"
#import "SmallImage.h"
#import "NetRequestModel.h"
#import "UIImageView+WebCache.h"
#import "MYViewController.h"
#import "AppDelegate.h"
static
@interface CollectionView()



@end

@implementation CollectionView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"imagesUrl.plist" ofType:nil]];
        if (self) {
            
        for (int i = 0; i < 12; i++) {
            
            NSDictionary *dict = array[i];
            
            self.smallImage = [[SmallImage alloc]init];
            
            UITapGestureRecognizer *tapRecognizer = nil;
            tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapDo:)];
            [self.smallImage addGestureRecognizer:tapRecognizer];
            
            NetRequestModel *model = [[NetRequestModel alloc]init];
            model.ID = i;
            model.date = @"day";
            model.dataFormat = @"json";
            
            self.smallImage.model = model;
            self.smallImage.tag = i;
            [self.smallImage.imageView setImageWithURL:[NSURL URLWithString:dict[@"imageurl"]] placeholderImage:[UIImage imageNamed:@"1.png"]];
            self.smallImage.iconName.text = dict[@"iconname"];
            self.smallImage.month.text = dict[@"month"];
            [self addSubview:self.smallImage];
            
        }
    }
    return self;
}

- (void)tapDo:(UITapGestureRecognizer *)recongnizer
{
    
    
    if ([self.delegate respondsToSelector:@selector(mystarBeclick:NetModle:)]) {
        self.smallImage = (SmallImage *)recongnizer.view;
        [self.delegate mystarBeclick:self NetModle:self.smallImage.model];
        
        AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
        delegate.imageID=self.smallImage.tag;
        NSLog(@"%ld",(long)delegate.imageID);
    
    }
   
       
    
   
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    //设置12个小图片的frame
    
    for (UIImageView *imageview in self.subviews) {
        //
        CGFloat imageX = imageview.tag % 4 * (Margin + imageWH) + Margin;
        CGFloat imageY = imageview.tag / 4 * (Margin + imageWH + 40) + Margin;
        imageview.frame =  CGRectMake(imageX, imageY, imageWH, imageWH + 2 * labelH);
    }
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
